export default {
	//数据
	data() {
		return {
			curId: 1,
      validate: {}, //验证对象

		}
	},
	created() { //调用方法
	},
	mounted() {
    jQuery.validator.addMethod("isMobile", function(value, element) {
      let length = value.length;
      let mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
      return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写手机号码");
    this.validate = $("#senForm").validate({
      ignore: [],
      rules: {
        name: { // 名称
          required: true,
          maxlength: 50
        },
        identityCard: { // 身份证
          required: true,
          maxlength: 18,
          minlength: 18
        },
        industry: { // 邮箱
          required: true,
        },
        phone: { // 性别
          required: true,
          isMobile : true
        },
      },
      messages: {
        name: { // 名称
          required: '请输入学员名称',
          maxlength: '最大长度为50'
        },
        identityCard: { // 身份证
          required: '请输入身份证号!',
          maxlength: '请输入正确的身份证',
          minlength: '请输入正确的身份证'
        },
        industry: { // 邮箱
          required: '请输入从事行业!',
        },
        phone: { // 性别
          required: '请输入联系方式!',
          isMobile: '请输入正确的联系方式!'
        },
      },
     /* errorElement: "em",
      errorPlacement: function(error, element) {
        error.appendTo(element.parents(".error-box").children(".error-msg"));
      }*/
    });
	},
	methods: { //写函数方法
     // 提交申请
	  subForm: function (){
      let that = this;
      let flg = that.validate.form();
      if(!flg){return;};

      let dataObj = that.combinationFormObject(that.validate.currentForm);
      // 身份证验证
      let obj = {"identityCard": dataObj.identityCard  };
      let ob = { "userAccount":dataObj.identityCard  };
      // 验证手机号码
      let phone = {"phone": dataObj.phone  };
      let userPhone = { "userPhoneNumber":dataObj.phone  };
      that.$post(
        that.urls.queryStudentHeavy,
        obj
      )
        .then(function(res) {

          if(res.success && res.data) {
            layer.msg('身份证已存在，请重新输入份证!', {
              time: 3000, //3s后自动关闭
            });
          }else {
            // 验证用户表是否重复
            that.$post(
              that.urls.queryHeavy,
              ob
            )
              .then(function(res) {

                if(res.success) {
                  layer.msg('身份证已存在,请重新输入!', {
                    time: 3000, //3s后自动关闭
                  });
                }else {
                 // 验证手机号码
                  that.$post(
                    that.urls.queryStudentHeavy,
                    phone
                  )
                    .then(function(res) {

                      if(res.success && res.data) {
                        layer.msg('手机号码已存在，请重新输入手机号码!', {
                          time: 3000, //3s后自动关闭
                        });
                      }else {
                       // 验证user表 手机号码

                        that.$post(
                          that.urls.queryHeavy,
                          userPhone
                        )
                          .then(function(res) {
                            if(res.success) {
                              layer.msg('手机号码已存在，请重新输入手机号码!', {
                                time: 3000, //3s后自动关闭
                              });
                            }else {

                              // submit

                              that.$post(
                                that.urls.studentRegistered,
                                dataObj
                              )
                                .then(function(res) {
                                  if(res.success) {

                                    layer.msg('提交申请成功！', {
                                      time: 3000, //3s后自动关闭
                                    });
                                    $('#theTrainingDiv input').val("");
                                    $('#theTrainingDiv input.input-box-btn').val("提交注册申请");
                                    that.curId = 0;
                                  }else {
                                    layer.msg('提交申请失败！', {
                                      time: 3000, //3s后自动关闭
                                    });
                                  }
                                })

                            }

                          })


                      }

                    })

                }
              })

          }

        })
    },

    // login
    login: function () {
	    let that = this;
	   let userAccount = $('#userAccount').val();
      let userPassword = $('#userPassword').val();
        if(!userAccount){
          layer.msg('请输入登录账号！', {
            time: 3000, //3s后自动关闭
          });
          return;
        }
      if(!userPassword){
        layer.msg('请输入登录密码！', {
          time: 3000, //3s后自动关闭
        });
        return;
      }

      that.$post(
        that.urls.platformLogin,
        {"userAccount": userAccount, "userPassword": userPassword}
      )
        .then(function(res) {
          if(res.success) {

            if(res.data.userInfo.userAccountStatus == 0){
              layer.msg('账号被冻结！', {
                time: 3000, //3s后自动关闭
              });
              return;
            }

            if(res.data.role.roleStatus == 0){
              layer.msg('账号角色被冻结！', {
                time: 3000, //3s后自动关闭
              });
              return;
            }

            layer.msg('登录成功！', {
              time: 3000, //3s后自动关闭
            });

            //将用户信息存入sessionStorage
            that.set("token", res.data.token);//token
            that.set("userRole", res.data.role);//角色信息
            that.set("userInfo", res.data.userInfo);//用户信息

            that.jumpPage();

          }else {
            layer.msg('登录失败！', {
              time: 3000, //3s后自动关闭
            });
          }
        })


    },

		/*返回到登录页*/
		jumpPage: function() {
			this.$router.push({
				path: "/index",
			})
		},

	}
}
